# python -m project_root.api.api_monitoring
from .api_token import Token 
from util.time_util import timestran
import requests

class Api_Monitoring(Token):


    def get_deivce_info(self,parameter='title'):
        url = 'http://hxzhan.rtcloud.com:32111/tm/realtime/device'
        json = {
            'device_id':57714
        }
        response = requests.post(url=url,json=json,headers=self.get_header())
        data = response.json()['data']
        if parameter == 'title':
            return data['app_scen_name']+'-'+data['name']
        elif parameter == 'time':
            if data['refresh_at'] == 0:
                return '-'
            else:
                return timestran(data['refresh_at'])
        elif parameter == 'state':
            if data['online_status'] == 1:
                return '在线'
            elif data['online_status'] == 2:
                return '离线'
            elif data['online_status'] == 3:
                return '部分离线'
        elif parameter == 'runstate':
            runstates = []
            for state  in data['alarm_infos']:
                if state['hase_alarm'] == False:
                    runstates.append('正常')
                else:
                    runstates.append('异常')
            return runstates
        elif parameter == 'vals':
            values = []
            for val in data['vals']:
                values.append(val['val'])
            return values
        

    def get_alarmlist(self):
        try:
            url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
            json = {
                'limit':6,
                'page':1,
                'device_id':57714,

            }
            keys = ['alarm_at','desc','level','return_status']
            response = requests.post(url=url,headers=self.get_header(),json=json)
            assert response.status_code == 200
            self.logger.info("调用接口 get_alarmlist 成功 ")
            values = []
            for data in response.json()['data']['list']:
                for key in keys:
                    if key == 'alarm_at':
                        values.append(timestran(data[key]))
                    elif key == 'level':
                        if data[key] == 1:
                            values.append('预警')
                        elif data[key] == 2:
                            values.append('报警')
                    elif key == 'return_status':
                        if data[key] == 1:
                            values.append('已复归')
                        elif data[key] == 2:
                            values.append('未复归')
                    else:
                        values.append(data[key])
            return values
        except Exception as e:
            self.logger.error("调用接口 get_alarmlist 失败",e)

# api_test = Api_Monitoring()
# print(api_test.get_alarmlist())
